﻿var move_obj_top;
var StartX_top;
var StartY_top;
var StartX2_top;//用于速度计算
var MoveX_top=null;
var flag_direction=true;
var timerCount = 0;
var moves_left;
var totalWidth;
var temPos; //滚动元素当前位置
var height_of_Typetop=0;
var flag_type_load=false;
$(function(){
  move_obj_top= document.getElementById("huadong");

try{
     bindToScreening();  
   }
   catch (e)
   {
       //alert("拖拽事件不支持");
   }
    
   function bindToScreening(){
       move_obj_top.addEventListener("touchstart",touchstartfunC,false);
       move_obj_top.addEventListener("touchmove",touchmovefunC,false);
       move_obj_top.addEventListener("touchend",touchendfunC,false);
   } 
   
   function touchstartfunC(e) {

     moves_left=Number(move_obj_top.offsetLeft);
  try {
          StartX_top= Number(e.touches[0].pageX);   //记录按下时的横坐标
          StartY_top= Number(e.touches[0].pageY);
          StartX2_top=StartX_top;
      }
      catch(e){

      }
      
   }
   
   function touchmovefunC(e) {

  try {
        e.preventDefault();
         
        if(MoveX_top!=null)
         { 
           if(Number(e.touches[0].pageX)>MoveX_top)
           {
              if(flag_direction==true)
                  {
                   timerCount=0;
                   StartX2_top=Number(e.touches[0].pageX);
                   }
                   else  timerCount++;  
                 flag_direction = false;
           }
           else
           {
              if(flag_direction==false)
                  {
                  timerCount=0;
                  StartX2_top=MoveX_top;
                  }
                  else  timerCount++; 

                  flag_direction=true;
           }

         }

 
        MoveX_top = Number(e.touches[0].pageX);   //记录移动时的横坐标
         if(MoveX_top>StartX_top) 
         {     
        
           if((moves_left + (MoveX_top-StartX_top))<=0)
            move_obj_top.style.left=moves_left + (MoveX_top-StartX_top)+"px";

         }
         else
         {
            if((moves_left + (MoveX_top-StartX_top))>=(-totalWidth))
          move_obj_top.style.left=moves_left - (StartX_top-MoveX_top)+"px";
         }

         switchPosition();
      }
      catch(e){
      }
   }

   function touchendfunC(e) {


   try {
          moves_left=Number(move_obj_top.offsetLeft);
          EndX = MoveX_top;


            if(flag_direction == true)
              {
                var speed = Number((StartX2_top-EndX)/timerCount);

                 startMoveSWitch(move_obj_top,"left",moves_left-parseInt(speed*3));
              }
              else if(flag_direction==false)
              {
                 var speed = Number((EndX-StartX2_top)/timerCount);

                 startMoveSWitch(move_obj_top,"left",moves_left+parseInt(speed*3));
              }

         timerCount = 0;
      }
      catch(e){

      }
   }
});


function bindEvents_(){
  

  for(var i=0;i<$(".fragment").length;i++)
  {
   
     $(".fragment").eq(i).click(function(){
        var indexii = $(".fragment").index(this);
          if(indexii<(Number_of_type-4))
         {
          startMove(move_obj_top,"left",parseInt(-window.innerWidth/4*indexii));
         }
         else
         {
           startMove(move_obj_top,"left",parseInt(-totalWidth));
         }

                   $(".fragment").eq(indexii).addClass("current").siblings().removeClass("current");
     })
  }
}

/*运动框架，包括链式运动框架,iTarget不带单位*/
function startMoveSWitch(obj, attr, iTarget, fn)
{
  var left = obj.offsetLeft;

  clearInterval(obj.timer);
  obj.timer=setInterval(function (){
    //1.取当前的值
    var iCur=0;
    
    if(attr=='opacity')
    {
      iCur=parseInt(parseFloat(getStyle(obj, attr))*100);
    }
    else
    {
      iCur=parseInt(getStyle(obj, attr));
    }


    //2.算速度
    var iSpeed = (iTarget-iCur)/5;
    iSpeed=iSpeed>0?Math.ceil(iSpeed):Math.floor(iSpeed);
    
    //3.检测停止
    if(iCur==iTarget)
    {
      clearInterval(obj.timer);

      if(fn)
      {
        fn();
      }
    }
    else
    {
      if(attr=='opacity')
      {
        obj.style.filter='alpha(opacity:'+(iCur+iSpeed)+')';
        obj.style.opacity=(iCur+iSpeed)/100;
      }
      else
      {
        obj.style[attr]=iCur+iSpeed+'px';
      }
    }

   if((iCur+iSpeed)>=0)
   {
    obj.style.left=0+"px";
    if(iTarget>=0)
    {
      iTarget = 0;
      clearInterval(obj.timer);
    }
   }
   else if((iCur+iSpeed)<=-totalWidth)
   {
    obj.style.left=(-totalWidth+"px");
    if(iTarget<=(-totalWidth))
    {
      iTarget = (-totalWidth);
      clearInterval(obj.timer);
    }
   }

  }, 30)
}

  function changeHeightMySelf(){
   var totalHeight = 0;
   var marginTop = 0;
   var paddingTop = 0;
   var marginBottom=0;
   var paddingBottom=0;
  $(".fragment").eq(0).children().each(function(indexI,element){

         var ds = parseInt($(element).css("height"));
         var ds1 = parseInt($(element).css("padding-top"));
         var ds2 = parseInt($(element).css("padding-bottom"));
         var ds3= parseInt($(element).css("margin-top"));
         var ds4 = parseInt($(element).css("margin-bottom"));
         //alert(ds+" ,"+ds1+" ,"+ds2+" ,"+ds3+" ,"+ds4);
         totalHeight+=(ds+ds1+ds2+ds3+ds4);

  });
  $(".daohan").css("height",totalHeight+"px");
}  